package com.sxbbc.agent.controller;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.agent.common.MappingUtils;
import com.sxbbc.agent.service.AgentBaseService;
import com.sxbbc.agent.service.AgentDataService;
import com.sxbbc.agent.util.AgentSession;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.encrypts.des.DESEncode;
import com.sxbbc.common.basics.utils.validcode.VerifyCode;
import com.sxbbc.common.core.entity.jz.AgAccount;
import com.sxbbc.common.core.entity.jz.AgAgent;
import com.sxbbc.common.core.entity.jz.AgMenus;
import com.sxbbc.common.core.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基础服务 控制层
 * 登录...
 * @author leehao
 * @date 2019-07-10
 */
@Controller
public class BaseController {

    @Autowired
    private AgentBaseService agentBaseService;
    @Autowired
    private IAgAgentService iAgAgentService;
    @Autowired
    private IAgAccountService iAgAccountService;
    @Autowired
    private AgentDataService agentDataService;

    @RequestMapping(value = MappingUtils.SHOW_LOGIN)
    public ModelAndView showLogin(){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_LOGIN);
        return view;
    }

    @RequestMapping(value = MappingUtils.VERIFY_IMAGE)
    public void verifyImg(HttpServletRequest request, HttpServletResponse response){
        VerifyCode verifyCode = VerifyCode.getInstance();
        verifyCode.createValidimg(response);
        AgentSession.setValidCode(request,verifyCode.getText());
    }

    @RequestMapping(value = MappingUtils.TO_LOGIN)
    @ResponseBody
    public RestResponse doLogin(HttpServletRequest request,String login,
                                String passWord,String validCode){
        String valid = AgentSession.getValidCode(request);
        if (StringUtils.isBlank(login)) {
            return GetRest.getFail("请输入用户名");
        }
        if (StringUtils.isBlank(passWord)) {
            return GetRest.getFail("请输入密码");
        }
        if (StringUtils.isBlank(validCode)) {
            return GetRest.getFail("请输入验证码");
        }
        if (!StringUtils.equalsIgnoreCase(valid, validCode)) {
            return GetRest.getFail("验证码不正确");
        }
        return agentBaseService.doLogin(login,passWord,request);
    }

    @RequestMapping(value = MappingUtils.SHOW_HOME)
    public ModelAndView showHome(HttpServletRequest request){
        List<AgMenus> menu = AgentSession.getMenu(request);
        AgAgent agent = AgentSession.getAgent(request);
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_HOME);
        view.addObject("list",menu);
        view.addObject("agent",agent);
        return view;
    }
    
    /**
     * 
     * 主页
     * @return
     * @author Wahyee
     */
    @RequestMapping(value = MappingUtils.SHOW_WELCOME)
    public ModelAndView showWelcome(HttpServletRequest request) {
    	ModelAndView view = new ModelAndView(MappingUtils.SHOW_WELCOME);
    	AgAgent agent = AgentSession.getAgent(request);
    	AgAccount account = iAgAccountService.getById(agent.getId());
        view.addObject("account", account);
        //统计数据
        agentDataService.queryStatisticalDataByLocation(agent,view);
    	return view;
    }

    @GetMapping(value = MappingUtils.SHOW_UPDATE_PASSWORD)
    public ModelAndView showUpdatePassword(HttpServletRequest request){
        AgAgent agent = AgentSession.getAgent(request);
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_UPDATE_PASSWORD);
        view.addObject("id",agent.getId());
        return view;
    }

    @RequestMapping(value = MappingUtils.UPDATE_PASSWORD)
    @ResponseBody
    public RestResponse updatePassword(Integer id,String oldPassword,String newPasswordOne,String newPasswordTwo){
        AgAgent agent = iAgAgentService.getById(id);
        if (agent == null) {
            return GetRest.getFail("该代理不存在");
        }
        if (StringUtils.isBlank(oldPassword)) {
            return GetRest.getFail("请输入原密码");
        }
        if (StringUtils.isBlank(newPasswordOne) || StringUtils.isBlank(newPasswordTwo)) {
            return GetRest.getFail("请输入新密码");
        }
        if (newPasswordOne.length() < 6 || newPasswordTwo.length() < 6) {
            return GetRest.getFail("密码长度不能少于6位");
        }
        DESEncode des = DESEncode.getInstance();
        String oldPwd = des.encrypt(oldPassword);
        if (!StringUtils.equals(oldPwd,agent.getPassWord())) {
            return GetRest.getFail("原密码不正确");
        }
        String newOne = des.encrypt(newPasswordOne);
        String newTwo = des.encrypt(newPasswordTwo);
        if (!StringUtils.equals(newOne,newTwo)) {
            return GetRest.getFail("两次输入的新密码不一致");
        }
        agent.setPassWord(newOne);
        boolean save = iAgAgentService.updateById(agent);
        if (!save) {
            throw new ThrowJsonException("密码修改失败");
        }
        return GetRest.getSuccess("修改成功");
    }

    @RequestMapping(value = MappingUtils.EXIT)
    public ModelAndView exit(HttpServletRequest request){
        AgentSession.exit(request);
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_LOGIN);
        return view;
    }
}
